import { query,add,remove,edit ,editStatus,editReward,queryOneUsergroup,editLevel,queryReward } from '@/services/downlines';
import {toTree} from './../../../utils/utils'
export default {
  namespace: 'downlines',
  state: {
    pageLoaded:false,
    data:[],
    dataList:[],
    scrollData:{x:0,y:0},
    selectedRowKeys: [],
    expandedRowKeys:[],
    parent_id:'',
    reward_commissions:{},
    searchParams:{
      license_plate_number:'',
      mobile:'',
      status:'0',
      page: 1,
      per_page: 30,
    },
    meta: {total: 0, per_page: 0,current_page:1},
    loading: true,
    editRecord:{}
  },
  effects: {
    *reset({ payload, callback }, { call, put }) {
     console.log('reset')
      yield put({
        type: 'resetData',
        payload:payload
      });
      if(callback) callback()
    },
    *changeLoaded({ payload, callback }, { call, put }) {
      yield put({
        type: 'changePageLoaded',
        payload: true,
      });
    },
    *changeScroll({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeScrollData',
        payload: payload,
      });
    },
    *selectRow({payload, callback}, {call, put,select}) {
      const {selectedRowKeys} = yield select(state => state.downlines)
      if (selectedRowKeys.indexOf(payload.id) >= 0) {
        selectedRowKeys.splice(selectedRowKeys.indexOf(payload.id), 1);
      } else {
        selectedRowKeys.push(payload.id);
      }
      yield put({
        type: 'saveSelectRow',
        payload: selectedRowKeys,
      });
    },
    *fetchReward({ payload,callback }, { call, put ,select}) {
      const response = yield call(queryReward, {...payload,project_id:'1'});
      if(response.status===200){
        yield put({
          type: 'saveReward',
          payload:response.data.data,
        });
      }
    },
    *fetch({ payload,callback }, { call, put ,select}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const state = yield select(state => state.downlines)//获取state中的数据
      payload=payload||state.searchParams;
      const response = yield call(query, {...payload,project_id:'1'});
      console.log(response)
      if(response.status===200){
        let preDate=JSON.parse(JSON.stringify([...state.dataList,...response.data.data]))
        console.log('preDate',toTree(preDate));
        yield put({
          type: 'save',
          payload: toTree(preDate),
          dataList:preDate
        });
        yield put({
          type: 'saveSearchParams',
          payload: payload
        });
        yield put({
          type: 'changeLoading',
          payload: false,
        });
        if (callback) callback();
      }

    },
    *changeCommission({ payload ,callback}, { call, put,select }) {
      const state = yield select(state => state.downlines)//获取state中的数据
      let predataList=JSON.parse(JSON.stringify([...state.dataList]))
      for(let i=0;i<predataList.length;i++){
        if(predataList[i].id==payload.id){
          predataList[i].initial_commission=payload.initial_commission;
          if(predataList[i].is_promoted==-1 || payload.level===1){
            predataList[i].current_commission=payload.initial_commission;
          }
          if(payload.level===2){
            predataList[i].promotion_commission=payload.promotion_commission;
            predataList[i].promotion_conditions=payload.promotion_conditions;
          }
        }
      }
      let preDate=toTree(predataList)
      console.log('preDate',preDate)
      console.log('predataList',predataList)
      yield put({
        type: 'save',
        payload: preDate,
        dataList:predataList
      });

    },
    *handleChangeStatus({ payload ,callback}, { call, put,select }) {
      const state = yield select(state => state.downlines)//获取state中的数据
      let predataList=JSON.parse(JSON.stringify([...state.dataList]))
      for(let i=0;i<predataList.length;i++){

        if(predataList[i].id===payload.id){
          console.log(predataList[i].id,payload.status)
          predataList[i].status=payload.status
        }
      }
      console.log('predataList',predataList)
      let preDate=toTree(predataList)
      console.log('preDate',preDate);
      yield put({
        type: 'save',
        payload: preDate,
        dataList:predataList
      });

    },
    *fetchOneusergroup({ payload ,callback}, { call, put }) {
      const response = yield call(queryOneUsergroup, payload);
      console.log(response)
      yield put({
        type: 'saveOneRecord',
        payload:  response.data
      });
      if(response.status===200){
        if (callback) callback();
      }
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(add, payload);

      console.log(response)
      if(response.status===200){
        if (callback) callback();
      }else{
      }
    },
    *editReward({ payload, callback }, { call, put }) {
      const response = yield call(editReward, {...payload,project_id:'1'});
      console.log(response)
      if(response.status===200){
        if (callback) callback();
      }
    },
    *edit({ payload, callback }, { call, put }) {
      console.log('edit')
      const response = yield call(edit, {...payload,project_id:'1'});
      console.log(response)
      if(response.status===200){
        if (callback) callback();
      }
    },
    *editLevel({ payload, callback }, { call, put }) {
      const response = yield call(editLevel, {...payload,project_id:1});

      console.log(response)
      if(response.status===200){
        if (callback) callback();
      }else{
      }
    },
    *changeLevelCommission({ payload ,callback}, { call, put,select }) {
      const state = yield select(state => state.downlines)//获取state中的数据
      let predataList=JSON.parse(JSON.stringify([...state.dataList]));
      for(let i=0;i<predataList.length;i++){
        if(predataList[i].level==payload.level){
          predataList[i].initial_commission=payload.initial_commission;
          predataList[i].promotion_commission=payload.promotion_commission;
          predataList[i].promotion_conditions=payload.promotion_conditions;
          if(predataList[i].is_promoted==-1 || payload.level==='1'){
            predataList[i].current_commission=payload.initial_commission;
          }
        }
      }
      let preDate=toTree(predataList)
      console.log('preDate',preDate)
      console.log('predataList',predataList)
      yield put({
        type: 'save',
        payload: preDate,
        dataList:predataList
      });

    },
    *changeSelectRow({payload, callback}, {call, put,select}) {
      yield put({
        type: 'saveSelectRow',
        payload: payload,
      });
    },
    *changeExpandedRowKeys({payload, callback}, {call, put,select}) {
      const state = yield select(state => state.downlines)//获取state中的数据
      let expandedRowKeys=JSON.parse(JSON.stringify([...state.expandedRowKeys]))
      console.log('expandedRowKeys',expandedRowKeys)
      if (payload.expanded) {
        expandedRowKeys.push(payload.id)
      } else {
        const index = expandedRowKeys.indexOf(payload.id);
        expandedRowKeys.splice(index,1)
      }
      console.log('expandedRowKeys',expandedRowKeys)
      yield put({
        type: 'saveExpandedRowKeys',
        payload: expandedRowKeys,
      });
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(remove, payload);
      if(response.status===200){
        if (callback) callback();
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
        dataList:action.dataList
      };
    },
    saveReward(state, action) {
      return {
        ...state,
        reward_commissions: action.payload,
      };
    },
    saveOneRecord(state, action) {
      return {
        ...state,
        editRecord:action.payload
      };
    },
    saveAndPush(state, action) {
      // console.log('[...state.data,...action.payload.data]',[...state.data,...action.payload.data]);
      const data=[...state.data,...action.payload.data];
      // data.map((item,index)=>{
      //   item.index=index
      // })
      return {
        ...state,
        data: data,
        meta:action.payload.meta
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    changePageLoaded(state, action) {
      return {
        ...state,
        pageLoaded: action.payload,
      };
    },
    changeScrollData(state, action) {
      return {
        ...state,
        scrollData: action.payload,
      };
    },
    saveSearchParams(state, action) {
      return {
        ...state,
        searchParams: action.payload,
      };
    },
    resetData(state, action) {
      return  {
        pageLoaded:action.payload,
        data:[],
        dataList:[],
        reward_commissions:{},
        scrollData:{x:0,y:0},
        selectedRowKeys: [],
        expandedRowKeys:[],
        parent_id:'',
        searchParams:{
          license_plate_number:'',
          mobile:'',
          status:'0',
          page: 1,
          per_page: 30,
        },
        meta: {total: 0, per_page: 0,current_page:1},
        loading: true,
        editRecord:{}
      };
    },
    saveSelectRow(state, action) {
      return {
        ...state,
        selectedRowKeys: action.payload,
      };
    },
    saveExpandedRowKeys(state, action) {
      return {
        ...state,
        expandedRowKeys: action.payload,
      };
    },
  },

};
